<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
namespace app\services\care;


use app\dao\care\ServiceLogDao;
use app\model\care\ServiceLog;
use app\services\BaseServices;

/**
 * Class ServiceLogServices
 * @package app\services\care
 */
class ServiceLogServices extends BaseServices
{
    /**
     * ServiceLogServices constructor.
     * @param ServiceLogDao $dao
     */
    public function __construct(ServiceLogDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 添加日志
     * @param int $obj_id 对象id
     * @param int $obj_type 对象类型：1用户操作，2订单操作，3消息提醒
     * @param string $log_content 日志说明
     * @param int $op_user_type 操作人类型： 1 用户 2服务人员 3后台人员 4系统
     * @param int $operation_user_id 操作人id
     * @param string $operation_user_name 操作人姓名
     * @param int $operation_type 操作类型
     * 1 创建订单，2 用户取消订单，3 用户支付订单，4 用户申请退款，5 用户订单支付失败，6 用户使用优惠券，7 服务人员完成服务，8 下单成功通知，9 发送模板消息，10 订单指派模板消息
     * 11 被接单提醒，12 语音通知，13订单审核
     * 20 后台确认收款，21 后台指派订单，22 后台完成订单，23 后台取消订单，24 后台同意退款，25 后台退款申请驳回，26 后台修改订单，27 后台订单备注
     * @return mixed
     */
    public static function addLog(int $obj_id,int $obj_type,string $log_content,int $op_user_type,int $operation_user_id,string $operation_user_name,int $operation_type){
        $data = [
            'obj_id'=>$obj_id,
            'obj_type'=>$obj_type,
            'log_content'=>$log_content,
            'op_user_type'=>$op_user_type,
            'operation_user_id'=>$operation_user_id,
            'operation_user_name'=>$operation_user_name,
            'operation_type'=>$operation_type,
            'operation_time'=>time()
        ];
        $model = new ServiceLog();
        $res = $model->save($data);
        return $res;
    }

    /**
     * 获取日志
     * @param int $obj_id
     * @param int $obj_type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getLog(int $obj_id, int $obj_type ){
        $list = ServiceLog::where(['obj_id'=>$obj_id,'obj_type'=>$obj_type])->order('operation_time desc')->select()->toArray();
        $op_user_type = [
            1 => '用户',
            2 => '服务人员',
            3 => '后台人员',
            4 => '系统',
        ];
        foreach ($list as &$row) {
            $row['op_user_type_name'] = $op_user_type[$row['op_user_type']];
            $row['operation_time'] = $row['operation_time'] ? date('Y-m-d H:i:s',$row['operation_time']) : '---';
        }
        unset($row);
        return $list;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where,bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        $obj_type = [
            0 => '未知',
            1 => '用户操作',
            2 => '订单操作',
            3 => '消息提醒'
        ];
        $op_user_type = [
            0 => '未知',
            1 => '用户',
            2 => '服务人员',
            3 => '后台',
            4 => '系统'
        ];
        $operation_type = [
            0 => '未知',
            1 => '创建订单',
            2 => '用户取消订单',
            3 => '用户支付订单',
            4 => '用户申请退款',
            5 => '用户订单支付失败',
            6 => '用户使用优惠劵',
            7 => '服务人员完成服务',
            8 => '下单成功通知',
            9 => '发送模板消息',
            10 => '订单指派模板消息',
            11 => '被接单提醒',
            12 => '语音提醒',
            13 => '订单审核',
            20 => '后台确认收款',
            21 => '后台指派订单',
            22 => '后台完成订单',
            23 => '后台取消订单',
            24 => '后台同意退款',
            25 => '后台退款申请驳回',
            26 => '后台修改订单',
            27 => '后台订单备注',
        ];
        foreach ($list['list'] as &$item){
            $item['obj_type'] = $obj_type[$item['obj_type']];
            $item['op_user_type'] = $op_user_type[$item['op_user_type']];
            $item['operation_type'] = $operation_type[$item['operation_type']];
            $item['operation_time'] = date('Y-m-d H:i:s',$item['operation_time']);
        }
        return $list;
    }
}
